const exp = require('express'),
    util = require('../utilities'),
    fs = require('fs')
db = require('../db')
const router = exp.Router()


router.get('/api/answer1/:id', util.sign, (req, res) => {
    var id = req.params.id
    var fileName = `count/${id}.txt`
    fs.exists(fileName, (exists) => {
        if (exists) {
            fs.readFile(fileName, (error, data) => {
                var count = parseInt(data)
                count++
                if (!isNaN(count)) {
                    fs.writeFile(fileName, count, (error) => {
                        db.Question.update({ '_id': id }, { $set: { 'count1': count } }, (err) => {
                        })
                    })
                }
            })
        }
        else {
            fs.appendFile(fileName, '1', (error) => {
                var count = 1
                fs.writeFile(fileName, count, (error) => {
                    db.Question.update({ '_id': id }, { $set: { 'count1': count } }, (err) => {
                    })
                })
            })
        }
    })
})

router.get('/api/answer/:id', util.sign, (req, res) => {
    var id = req.params.id

    db.Question
        .find({ '_id': id }).select(' petname title _id time ele1 ele2 ele3 content answers count1 count2')
        .exec((err, data) => {
            if (err) {
                // 
            } else {
                var questions = [];
                res.json({
                    Data: data
                });
            }
        });
})

router.post('/api/answer', util.sign, (req, res) => {
    var petname = req.cookies.petname
    req.body.petname = petname
    req.body.ip = req.ip
    req.body.time = new Date()
    var id = req.body.id
    var fileName = `count1/${id}.txt`
    fs.exists(fileName, (exists) => {
        if (exists) {
            fs.readFile(fileName, (error, data) => {
                var count = parseInt(data)
                count++
                if (!isNaN(count)) {
                    fs.writeFile(fileName, count, (error) => {
                        db.Question.update({ '_id': id }, { $set: { 'count2': count } }, (err) => {
                        })
                        db.Question.findByIdAndUpdate(id, { $push: { answers: req.body } }, (err, data) => {
                            console.log(data)
                            util.send(res, 'success', '回复提交成功！')
                        })
                    })
                }
            })
        }
        else {
            fs.appendFile(fileName, '1', (error) => {
                var count = 1
                fs.writeFile(fileName, count, (error) => {
                    db.Question.update({ '_id': id }, { $set: { 'count2': count } }, (err) => {
                    })
                    db.Question.findByIdAndUpdate(id, { $push: { answers: req.body } }, (err, data) => {
                        console.log(data)
                        util.send(res, 'success', '回复提交成功！')
                    })
                })
            })
        }
    })
})

module.exports = router